package com.mpdbailey.cleverdicandroid;

import com.pigdogbay.androidutils.mvp.BackgroundColorPresenter;
import com.pigdogbay.androidutils.mvp.IBackgroundColorView;
import com.pigdogbay.androidutils.utils.ActivityUtils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener,IBackgroundColorView
{
	AppData _AppData;
	BackgroundColorPresenter _BackgroundColorPresenter;
   @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        setContentView(R.layout.activity_settings);
        _AppData = new AppData(this);
		_BackgroundColorPresenter = new BackgroundColorPresenter(this,_AppData.createBackgroundColorModel());
        _BackgroundColorPresenter.updateBackground();
    }
    @Override
    protected void onResume() {
    	super.onResume();
		PreferenceManager.getDefaultSharedPreferences(this)
		.registerOnSharedPreferenceChangeListener(this);
    }
    @Override
    protected void onPause() {
    	super.onPause();
		PreferenceManager.getDefaultSharedPreferences(this)
		.unregisterOnSharedPreferenceChangeListener(this);
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_settings, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId())
		{
		case (R.id.menu_settings_home):
			finish();
			break;
		default:
			return false;
		}
		return true;
	}    

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(getString(R.string.code_pref_background_colour))){
			_BackgroundColorPresenter.updateBackground();
		}
		else if (key.equals(getString(R.string.code_pref_use_large_dictionary_key))){
			onUseLargeDictionaryChange();
		}
		
	}
	private void onUseLargeDictionaryChange(){
		boolean hasUnlockLargeDictionaryPurchase = _AppData.getUnlockLargeDictionary();
		if (!hasUnlockLargeDictionaryPurchase &&_AppData.getUseLargeDictionary())
		{
			PreferenceManager.getDefaultSharedPreferences(this)
			.unregisterOnSharedPreferenceChangeListener(this);
			_AppData.getPreferencesHelper().setBoolean(R.string.code_pref_use_large_dictionary_key, false);
			PreferenceManager.getDefaultSharedPreferences(this)
			.registerOnSharedPreferenceChangeListener(this);
			ActivityUtils.showInfoDialog(this,R.string.settings_purchase_large_dictionary_title , R.string.settings_purchase_large_dictionary_message);
		}
	}
	@Override
	public void setBackgroundColor(int id) {
		ActivityUtils.setBackground(this, R.id.rootLayout, id);
	}
	@Override
	public void showPurchaseRequiredWarning() {
		ActivityUtils.showInfoDialog(this,R.string.settings_purchase_color_pack_title , R.string.settings_purchase_color_pack_message);
	}	
}
